package com.antfinancial.antchain.smallprogram;

import com.alipay.mychain.gl.biz.sdk.Client;
import com.alipay.mychain.gl.biz.sdk.DefaultClientPropertyConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;
import java.util.Properties;

@Configuration
public class ClientConfig {
    @Value("${ssl_key_password}")
    private String sslKeyPassword;
    @Value("${trust_password}")
    private String trustPassword;

//    class DefaultClientPropertyConfig extends ClientPropertyConfig {
//        DefaultClientPropertyConfig(Properties properties){
//            super(properties);
//        }
//        @Override
//        public InputStream getSslKeyStream(){
//            return MychainClient.class.getClassLoader().getResourceAsStream("client2_pkcs8.pem");
//        }
//        @Override
//        public InputStream getSslCertStream(){
//            return MychainClient.class.getClassLoader().getResourceAsStream("client2_cert.crt");
//        }
//        @Override
//        public InputStream getTrustStoreStream(){
//            return MychainClient.class.getClassLoader().getResourceAsStream("trust.keystore");
//        }
//    }
//    @Bean
//    public MychainClient getMychainClient(){
//        Properties properties=new Properties();
//        properties.setProperty("mychain.sslKeyPassword",sslKeyPassword);
//        properties.setProperty("mychain.trustStorePassword",trustPassword);
//        properties.setProperty("mychain.primary","47.97.73.208 10000");
//        properties.setProperty("mychain.backups","47.97.73.110 10000;47.97.73.157 10000;47.97.25.7 10000");
//        DefaultClientPropertyConfig defaultClientPropertyConfig=new DefaultClientPropertyConfig(properties);
//        MychainClient client=new MychainClient(defaultClientPropertyConfig);
//        return client;
//    }
    @Bean
    public Client getClient(){
        //{"backups":"47.107.216.93 8080;47.107.25.47 8080;47.107.25.182 8080","clientAlias":"测试链","primary":"47.107.27.122 8080","pullBlock":false,"sslCert":"be5442e0de45f0eb9ab9cd61a4842fa283936a1295af7bb0f6267d6d0b2729dd","sslKey":"aliyun_mychain-gl_baas_admin_pkcs8_9","sslKeyPassword":"mychain","trustStore":"aliyun_mychain-gl_baas_admin_truststore_9","trustStorePassword":"mychain"}
        Properties properties=new Properties();
        properties.setProperty("biz.sdk.primary","47.107.27.122 8080");
        properties.setProperty("biz.sdk.backups","47.107.216.93 8080;47.107.25.47 8080;47.107.25.182 8080");
        properties.setProperty("biz.sdk.trust_store_password","mychain");
        properties.setProperty("biz.sdk.ssl_key_password","mychain");
        DefaultClientPropertyConfig clientTest =new DefaultClientPropertyConfig(properties,ClientConfig.class.getClassLoader().getResourceAsStream("sslkey"),ClientConfig.class.getClassLoader().getResourceAsStream("sslcert"),ClientConfig.class.getClassLoader().getResourceAsStream("truststore"));
        //DefaultClientPropertyConfig clientTest =new DefaultClientPropertyConfig(properties,ClientTest.class.getClassLoader().getResourceAsStream("sslkey.pem"),ClientTest.class.getClassLoader().getResourceAsStream("sslcert.pem"),ClientTest.class.getClassLoader().getResourceAsStream("trust.keystore2"));
        Client client=new Client(clientTest);
        return client;
    }
}
